[llvm-commits] [llvm] r151093 - in /llvm/trunk: lib/Transforms/IPO/GlobalOpt.cpp test/Transforms/GlobalOpt/constantfold-initializers.ll

Nick Lewycky nicholas at mxc.ca
Tue Feb 21 22:25:23 PST 2012


Chris Lattner wrote:
>
> On Feb 21, 2012, at 2:08 PM, Nick Lewycky wrote:
>
>> Author: nicholas
>> Date: Tue Feb 21 16:08:06 2012
>> New Revision: 151093
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=151093&view=rev
>> Log:
>> Use the target-aware constant folder on expressions to improve the chance
>> they'll be simple enough to simulate, and to reduce the chance we'll encounter
>> equal but different simple pointer constants.
>>
>> This removes the symptoms from PR11352 but is not a full fix. A proper fix would
>> either require a guarantee that two constant objects we simulate are folded
>> when equal, or a different way of handling equal pointers (ie., trying a
>> constantexpr icmp on them to see whether we know they're equal or non-equal or
>> unsure).
>
> I think it should be enough to consider two constants the same if they are pointer equal and neither is a ConstantExpr.

Sure that's sufficient, but we'd like to handle ConstantExpr's, such as 
an all-zero GEP.

Yep, the non-TD constant folder can't pick one of:
   getelementptr inbounds (%closure* @f, i32 0, i32 0)
   getelementptr inbounds (%closure* @f, i64 0, i32 0)
. Oops, and that breaks GlobalOpt. Double-oops.

Really, I'd rather figure out some way to constant fold those into the 
same Constant* rather than teach globalopt anything new.

Nick

>
> -Chris
>
>>
>> Modified:
>>     llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
>>     llvm/trunk/test/Transforms/GlobalOpt/constantfold-initializers.ll
>>
>> Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=151093&r1=151092&r2=151093&view=diff
>> ==============================================================================
>> --- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
>> +++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Tue Feb 21 16:08:06 2012
>> @@ -2388,6 +2388,8 @@
>>      if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
>>        if (!SI->isSimple()) return false;  // no volatile/atomic accesses.
>>        Constant *Ptr = getVal(SI->getOperand(1));
>> +      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
>> +        Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
>>        if (!isSimpleEnoughPointerToCommit(Ptr))
>>          // If this is too complex for us to commit, reject it.
>>          return false;
>> @@ -2423,7 +2425,9 @@
>>                Constant * const IdxList[] = {IdxZero, IdxZero};
>>
>>                Ptr = ConstantExpr::getGetElementPtr(Ptr, IdxList);
>> -
>> +              if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
>> +                Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
>> +
>>              // If we can't improve the situation by introspecting NewTy,
>>              // we have to give up.
>>              } else {
>> @@ -2464,7 +2468,10 @@
>>                                         cast<GEPOperator>(GEP)->isInBounds());
>>      } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
>>        if (!LI->isSimple()) return false;  // no volatile/atomic accesses.
>> -      InstResult = ComputeLoadResult(getVal(LI->getOperand(0)));
>> +      Constant *Ptr = getVal(LI->getOperand(0));
>> +      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
>> +        Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
>> +      InstResult = ComputeLoadResult(Ptr);
>>        if (InstResult == 0) return false; // Could not evaluate load.
>>      } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
>>        if (AI->isArrayAllocation()) return false;  // Cannot handle array allocs.
>>
>> Modified: llvm/trunk/test/Transforms/GlobalOpt/constantfold-initializers.ll
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/constantfold-initializers.ll?rev=151093&r1=151092&r2=151093&view=diff
>> ==============================================================================
>> --- llvm/trunk/test/Transforms/GlobalOpt/constantfold-initializers.ll (original)
>> +++ llvm/trunk/test/Transforms/GlobalOpt/constantfold-initializers.ll Tue Feb 21 16:08:06 2012
>> @@ -6,3 +6,46 @@
>>
>> ; CHECK: @A = global i1 false
>> @A = global i1 icmp ne (i64 sub nsw (i64 ptrtoint (i8* getelementptr inbounds ([3 x i8]* @.str91250, i64 0, i64 1) to i64), i64 ptrtoint ([3 x i8]* @.str91250 to i64)), i64 1)
>> +
>> +; PR11352
>> +
>> + at xs = global [2 x i32] zeroinitializer, align 4
>> +; CHECK: @xs = global [2 x i32] [i32 1, i32 1]
>> +
>> +define internal void @test1() {
>> +entry:
>> +  store i32 1, i32* getelementptr inbounds ([2 x i32]* @xs, i64 0, i64 0)
>> +  %0 = load i32* getelementptr inbounds ([2 x i32]* @xs, i32 0, i64 0), align 4
>> +  store i32 %0, i32* getelementptr inbounds ([2 x i32]* @xs, i64 0, i64 1)
>> +  ret void
>> +}
>> +
>> +; PR12060
>> +
>> +%closure = type { i32 }
>> +
>> + at f = internal global %closure zeroinitializer, align 4
>> + at m = global i32 0, align 4
>> +; CHECK-NOT: @f
>> +; CHECK: @m = global i32 13
>> +
>> +define internal i32 @test2_helper(%closure* %this, i32 %b) {
>> +entry:
>> +  %0 = getelementptr inbounds %closure* %this, i32 0, i32 0
>> +  %1 = load i32* %0, align 4
>> +  %add = add nsw i32 %1, %b
>> +  ret i32 %add
>> +}
>> +
>> +define internal void @test2() {
>> +entry:
>> +  store i32 4, i32* getelementptr inbounds (%closure* @f, i32 0, i32 0)
>> +  %call = call i32 @test2_helper(%closure* @f, i32 9)
>> +  store i32 %call, i32* @m, align 4
>> +  ret void
>> +}
>> +
>> + at llvm.global_ctors = appending constant
>> +  [2 x { i32, void ()* }]
>> +  [{ i32, void ()* } { i32 65535, void ()* @test1 },
>> +   { i32, void ()* } { i32 65535, void ()* @test2 }]
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
>




More information about the llvm-commits mailing list