llvm.org GIT mirror llvm / 9423112
Merging r309651 and r309849: ------------------------------------------------------------------------ r309651 | inouehrs | 2017-07-31 20:32:15 -0700 (Mon, 31 Jul 2017) | 16 lines [StackColoring] Update AliasAnalysis information in stack coloring pass Stack coloring pass need to maintain AliasAnalysis information when merging stack slots of different types. Actually, there is a FIXME comment in StackColoring.cpp // FIXME: In order to enable the use of TBAA when using AA in CodeGen, // we'll also need to update the TBAA nodes in MMOs with values // derived from the merged allocas. But, TBAA has been already enabled in CodeGen without fixing this pass. The incorrect TBAA metadata results in recent failures in bootstrap test on ppc64le (PR33928) by allowing unsafe instruction scheduling. Although we observed the problem on ppc64le, this is a platform neutral issue. This patch makes the stack coloring pass maintains AliasAnalysis information when merging multiple stack slots. ------------------------------------------------------------------------ ------------------------------------------------------------------------ r309849 | inouehrs | 2017-08-02 11:16:32 -0700 (Wed, 02 Aug 2017) | 19 lines [StackColoring] Update AliasAnalysis information in stack coloring pass (part 2) This patch is update after the first patch (https://reviews.llvm.org/rL309651) based on the post-commit comments. Stack coloring pass need to maintain AliasAnalysis information when merging stack slots of different types. Actually, there is a FIXME comment in StackColoring.cpp // FIXME: In order to enable the use of TBAA when using AA in CodeGen, // we'll also need to update the TBAA nodes in MMOs with values // derived from the merged allocas. But, TBAA has been already enabled in CodeGen without fixing this pass. The incorrect TBAA metadata results in recent failures in bootstrap test on ppc64le (PR33928) by allowing unsafe instruction scheduling. Although we observed the problem on ppc64le, this is a platform neutral issue. This patch makes the stack coloring pass maintains AliasAnalysis information when merging multiple stack slots. This patch fixes PR33928. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@309957 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 3 years ago
7 changed file(s) with 145 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
310310 void GetUnderlyingObjects(Value *V, SmallVectorImpl &Objects,
311311 const DataLayout &DL, LoopInfo *LI = nullptr,
312312 unsigned MaxLookup = 6);
313
314 /// This is a wrapper around GetUnderlyingObjects and adds support for basic
315 /// ptrtoint+arithmetic+inttoptr sequences.
316 void getUnderlyingObjectsForCodeGen(const Value *V,
317 SmallVectorImpl &Objects,
318 const DataLayout &DL);
313319
314320 /// Return true if the only users of this pointer are lifetime markers.
315321 bool onlyUsedByLifetimeMarkers(const Value *V);
660660 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
661661 int64_t Offset, uint64_t Size);
662662
663 /// Allocate a new MachineMemOperand by copying an existing one,
664 /// replacing only AliasAnalysis information. MachineMemOperands are owned
665 /// by the MachineFunction and need not be explicitly deallocated.
666 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
667 const AAMDNodes &AAInfo);
668
663669 using OperandCapacity = ArrayRecycler::Capacity;
664670
665671 /// Allocate an array of MachineOperands. This is only intended for use by
378378 return NumMemRefs == 1;
379379 }
380380
381 /// Return the number of memory operands.
382 unsigned getNumMemOperands() const { return NumMemRefs; }
383
381384 /// API for querying MachineInstr properties. They are the same as MCInstrDesc
382385 /// queries but they are bundle aware.
383386
32763276 } while (!Worklist.empty());
32773277 }
32783278
3279 /// This is the function that does the work of looking through basic
3280 /// ptrtoint+arithmetic+inttoptr sequences.
3281 static const Value *getUnderlyingObjectFromInt(const Value *V) {
3282 do {
3283 if (const Operator *U = dyn_cast(V)) {
3284 // If we find a ptrtoint, we can transfer control back to the
3285 // regular getUnderlyingObjectFromInt.
3286 if (U->getOpcode() == Instruction::PtrToInt)
3287 return U->getOperand(0);
3288 // If we find an add of a constant, a multiplied value, or a phi, it's
3289 // likely that the other operand will lead us to the base
3290 // object. We don't have to worry about the case where the
3291 // object address is somehow being computed by the multiply,
3292 // because our callers only care when the result is an
3293 // identifiable object.
3294 if (U->getOpcode() != Instruction::Add ||
3295 (!isa(U->getOperand(1)) &&
3296 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
3297 !isa(U->getOperand(1))))
3298 return V;
3299 V = U->getOperand(0);
3300 } else {
3301 return V;
3302 }
3303 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
3304 } while (true);
3305 }
3306
3307 /// This is a wrapper around GetUnderlyingObjects and adds support for basic
3308 /// ptrtoint+arithmetic+inttoptr sequences.
3309 void llvm::getUnderlyingObjectsForCodeGen(const Value *V,
3310 SmallVectorImpl &Objects,
3311 const DataLayout &DL) {
3312 SmallPtrSet Visited;
3313 SmallVector Working(1, V);
3314 do {
3315 V = Working.pop_back_val();
3316
3317 SmallVector Objs;
3318 GetUnderlyingObjects(const_cast(V), Objs, DL);
3319
3320 for (Value *V : Objs) {
3321 if (!Visited.insert(V).second)
3322 continue;
3323 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
3324 const Value *O =
3325 getUnderlyingObjectFromInt(cast(V)->getOperand(0));
3326 if (O->getType()->isPointerTy()) {
3327 Working.push_back(O);
3328 continue;
3329 }
3330 }
3331 // If GetUnderlyingObjects fails to find an identifiable object,
3332 // getUnderlyingObjectsForCodeGen also fails for safety.
3333 if (!isIdentifiedObject(V)) {
3334 Objects.clear();
3335 return;
3336 }
3337 Objects.push_back(const_cast(V));
3338 }
3339 } while (!Working.empty());
3340 }
3341
32793342 /// Return true if the only users of this pointer are lifetime markers.
32803343 bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
32813344 for (const User *U : V->users()) {
329329 MMO->getOrdering(), MMO->getFailureOrdering());
330330 }
331331
332 MachineMemOperand *
333 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
334 const AAMDNodes &AAInfo) {
335 MachinePointerInfo MPI = MMO->getValue() ?
336 MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
337 MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
338
339 return new (Allocator)
340 MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
341 MMO->getBaseAlignment(), AAInfo,
342 MMO->getRanges(), MMO->getSyncScopeID(),
343 MMO->getOrdering(), MMO->getFailureOrdering());
344 }
345
332346 MachineInstr::mmo_iterator
333347 MachineFunction::allocateMemRefsArray(unsigned long Num) {
334348 return Allocator.Allocate(Num);
120120 SchedModel.init(ST.getSchedModel(), &ST, TII);
121121 }
122122
123 /// This is the function that does the work of looking through basic
124 /// ptrtoint+arithmetic+inttoptr sequences.
125 static const Value *getUnderlyingObjectFromInt(const Value *V) {
126 do {
127 if (const Operator *U = dyn_cast(V)) {
128 // If we find a ptrtoint, we can transfer control back to the
129 // regular getUnderlyingObjectFromInt.
130 if (U->getOpcode() == Instruction::PtrToInt)
131 return U->getOperand(0);
132 // If we find an add of a constant, a multiplied value, or a phi, it's
133 // likely that the other operand will lead us to the base
134 // object. We don't have to worry about the case where the
135 // object address is somehow being computed by the multiply,
136 // because our callers only care when the result is an
137 // identifiable object.
138 if (U->getOpcode() != Instruction::Add ||
139 (!isa(U->getOperand(1)) &&
140 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
141 !isa(U->getOperand(1))))
142 return V;
143 V = U->getOperand(0);
144 } else {
145 return V;
146 }
147 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
148 } while (true);
149 }
150
151 /// This is a wrapper around GetUnderlyingObjects and adds support for basic
152 /// ptrtoint+arithmetic+inttoptr sequences.
153 static void getUnderlyingObjects(const Value *V,
154 SmallVectorImpl &Objects,
155 const DataLayout &DL) {
156 SmallPtrSet Visited;
157 SmallVector Working(1, V);
158 do {
159 V = Working.pop_back_val();
160
161 SmallVector Objs;
162 GetUnderlyingObjects(const_cast(V), Objs, DL);
163
164 for (Value *V : Objs) {
165 if (!Visited.insert(V).second)
166 continue;
167 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
168 const Value *O =
169 getUnderlyingObjectFromInt(cast(V)->getOperand(0));
170 if (O->getType()->isPointerTy()) {
171 Working.push_back(O);
172 continue;
173 }
174 }
175 Objects.push_back(const_cast(V));
176 }
177 } while (!Working.empty());
178 }
179
180123 /// If this machine instr has memory reference information and it can be tracked
181124 /// to a normal reference to a known object, return the Value for that object.
182125 static void getUnderlyingObjectsForInstr(const MachineInstr *MI,
207150 Objects.push_back(UnderlyingObjectsVector::value_type(PSV, MayAlias));
208151 } else if (const Value *V = MMO->getValue()) {
209152 SmallVector Objs;
210 getUnderlyingObjects(V, Objs, DL);
153 getUnderlyingObjectsForCodeGen(V, Objs, DL);
211154
212155 for (Value *V : Objs) {
213 if (!isIdentifiedObject(V))
214 return false;
215
156 assert(isIdentifiedObject(V));
216157 Objects.push_back(UnderlyingObjectsVector::value_type(V, true));
217158 }
218159 } else
3636 #include "llvm/CodeGen/MachineRegisterInfo.h"
3737 #include "llvm/CodeGen/Passes.h"
3838 #include "llvm/CodeGen/PseudoSourceValue.h"
39 #include "llvm/CodeGen/SelectionDAGNodes.h"
3940 #include "llvm/CodeGen/SlotIndexes.h"
4041 #include "llvm/CodeGen/StackProtector.h"
4142 #include "llvm/CodeGen/WinEHFuncInfo.h"
888889
889890 // Keep a list of *allocas* which need to be remapped.
890891 DenseMap Allocas;
892
893 // Keep a list of allocas which has been affected by the remap.
894 SmallPtrSet MergedAllocas;
895
891896 for (const std::pair &SI : SlotRemap) {
892897 const AllocaInst *From = MFI->getObjectAllocation(SI.first);
893898 const AllocaInst *To = MFI->getObjectAllocation(SI.second);
907912 Inst = Cast;
908913 }
909914
915 // We keep both slots to maintain AliasAnalysis metadata later.
916 MergedAllocas.insert(From);
917 MergedAllocas.insert(To);
918
910919 // Allow the stack protector to adjust its value map to account for the
911920 // upcoming replacement.
912921 SP->adjustForColoring(From, To);
938947
939948 // Update the MachineMemOperand to use the new alloca.
940949 for (MachineMemOperand *MMO : I.memoperands()) {
941 // FIXME: In order to enable the use of TBAA when using AA in CodeGen,
942 // we'll also need to update the TBAA nodes in MMOs with values
943 // derived from the merged allocas. When doing this, we'll need to use
944 // the same variant of GetUnderlyingObjects that is used by the
945 // instruction scheduler (that can look through ptrtoint/inttoptr
946 // pairs).
947
948950 // We've replaced IR-level uses of the remapped allocas, so we only
949951 // need to replace direct uses here.
950952 const AllocaInst *AI = dyn_cast_or_null(MMO->getValue());
996998 MO.setIndex(ToSlot);
997999 FixedInstr++;
9981000 }
1001
1002 // We adjust AliasAnalysis information for merged stack slots.
1003 MachineSDNode::mmo_iterator NewMemOps =
1004 MF->allocateMemRefsArray(I.getNumMemOperands());
1005 unsigned MemOpIdx = 0;
1006 bool ReplaceMemOps = false;
1007 for (MachineMemOperand *MMO : I.memoperands()) {
1008 // If this memory location can be a slot remapped here,
1009 // we remove AA information.
1010 bool MayHaveConflictingAAMD = false;
1011 if (MMO->getAAInfo()) {
1012 if (const Value *MMOV = MMO->getValue()) {
1013 SmallVector Objs;
1014 getUnderlyingObjectsForCodeGen(MMOV, Objs, MF->getDataLayout());
1015
1016 if (Objs.empty())
1017 MayHaveConflictingAAMD = true;
1018 else
1019 for (Value *V : Objs) {
1020 // If this memory location comes from a known stack slot
1021 // that is not remapped, we continue checking.
1022 // Otherwise, we need to invalidate AA infomation.
1023 const AllocaInst *AI = dyn_cast_or_null(V);
1024 if (AI && MergedAllocas.count(AI)) {
1025 MayHaveConflictingAAMD = true;
1026 break;
1027 }
1028 }
1029 }
1030 }
1031 if (MayHaveConflictingAAMD) {
1032 NewMemOps[MemOpIdx++] = MF->getMachineMemOperand(MMO, AAMDNodes());
1033 ReplaceMemOps = true;
1034 }
1035 else
1036 NewMemOps[MemOpIdx++] = MMO;
1037 }
1038
1039 // If any memory operand is updated, set memory references of
1040 // this instruction.
1041 if (ReplaceMemOps)
1042 I.setMemRefs(std::make_pair(NewMemOps, I.getNumMemOperands()));
9991043 }
10001044
10011045 // Update the location of C++ catch objects for the MSVC personality routine.