aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-12-02 07:11:07 +0000
committerChris Lattner <sabre@nondot.org>2004-12-02 07:11:07 +0000
commitfa07e4fc302b1e274c292c2db4604e61d69458d5 (patch)
tree62781b170451325d81e02929c10e53a24d38e4a5 /lib
parentbc965b97ade56092b5fe4251eb68695271f1d82a (diff)
Implement a FIXME by checking to make sure that a malloc is not being used
in scary and unknown ways before we promote it. This fixes the miscompilation of 188.ammp that has been plauging us since a globalopt patch went in. Thanks a ton to Tanya for helping me diagnose the problem! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18418 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Transforms/IPO/GlobalOpt.cpp28
1 files changed, 25 insertions, 3 deletions
diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp
index db2d0b3535..0fde1d94a3 100644
--- a/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/lib/Transforms/IPO/GlobalOpt.cpp
@@ -738,6 +738,29 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
return NewGV;
}
+/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
+/// to make sure that there are no complex uses of V. We permit simple things
+/// like dereferencing the pointer, but not storing through the address, unless
+/// it is to the specified global.
+static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V,
+ GlobalVariable *GV) {
+ for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI)
+ if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) {
+ // Fine, ignore.
+ } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
+ if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
+ return false; // Storing the pointer itself... bad.
+ // Otherwise, storing through it, or storing into GV... fine.
+ } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI)) {
+ if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),GV))
+ return false;
+ } else {
+ return false;
+ }
+ return true;
+
+}
+
// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
// that only one value (besides its initializer) is ever stored to the global.
static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
@@ -783,9 +806,8 @@ static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
if (MI->getAllocatedType()->isSized() &&
NElements->getRawValue()*
TD.getTypeSize(MI->getAllocatedType()) < 2048 &&
- AllUsesOfLoadedValueWillTrapIfNull(GV)) {
- // FIXME: do more correctness checking to make sure the result of the
- // malloc isn't squirrelled away somewhere.
+ AllUsesOfLoadedValueWillTrapIfNull(GV) &&
+ ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV)) {
GVI = OptimizeGlobalAddressOfMalloc(GV, MI);
return true;
}