1 //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h" 11 #include "llvm/CodeGen/ISDOpcodes.h" 12 #include "llvm/CodeGen/MachineFrameInfo.h" 13 #include "llvm/CodeGen/MachineFunction.h" 14 #include "llvm/CodeGen/SelectionDAG.h" 15 #include "llvm/CodeGen/SelectionDAGNodes.h" 16 #include "llvm/CodeGen/TargetLowering.h" 17 #include "llvm/Support/Casting.h" 18 #include <cstdint> 19 20 using namespace llvm; 21 22 bool BaseIndexOffset::equalBaseIndex(BaseIndexOffset &Other, 23 const SelectionDAG &DAG, int64_t &Off) { 24 // Initial Offset difference. 25 Off = Other.Offset - Offset; 26 27 if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) { 28 // Trivial match. 29 if (Other.Base == Base) 30 return true; 31 32 // Match GlobalAddresses 33 if (auto *A = dyn_cast<GlobalAddressSDNode>(Base)) 34 if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base)) 35 if (A->getGlobal() == B->getGlobal()) { 36 Off += B->getOffset() - A->getOffset(); 37 return true; 38 } 39 40 const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 41 42 // Match non-equal FrameIndexes - If both frame indices are fixed 43 // we know their relative offsets and can compare them. Otherwise 44 // we must be conservative. 45 if (auto *A = dyn_cast<FrameIndexSDNode>(Base)) 46 if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) 47 if (MFI.isFixedObjectIndex(A->getIndex()) && 48 MFI.isFixedObjectIndex(B->getIndex())) { 49 Off += MFI.getObjectOffset(B->getIndex()) - 50 MFI.getObjectOffset(A->getIndex()); 51 return true; 52 } 53 } 54 return false; 55 } 56 57 /// Parses tree in Ptr for base, index, offset addresses. 58 BaseIndexOffset BaseIndexOffset::match(SDValue Ptr, const SelectionDAG &DAG) { 59 // (((B + I*M) + c)) + c ... 60 SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr); 61 SDValue Index = SDValue(); 62 int64_t Offset = 0; 63 bool IsIndexSignExt = false; 64 65 // Consume constant adds & ors with appropriate masking. 66 while (Base->getOpcode() == ISD::ADD || Base->getOpcode() == ISD::OR) { 67 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) { 68 // Only consider ORs which act as adds. 69 if (Base->getOpcode() == ISD::OR && 70 !DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) 71 break; 72 Offset += C->getSExtValue(); 73 Base = Base->getOperand(0); 74 continue; 75 } 76 break; 77 } 78 79 if (Base->getOpcode() == ISD::ADD) { 80 // TODO: The following code appears to be needless as it just 81 // bails on some Ptrs early, reducing the cases where we 82 // find equivalence. We should be able to remove this. 83 // Inside a loop the current BASE pointer is calculated using an ADD and a 84 // MUL instruction. In this case Base is the actual BASE pointer. 85 // (i64 add (i64 %array_ptr) 86 // (i64 mul (i64 %induction_var) 87 // (i64 %element_size))) 88 if (Base->getOperand(1)->getOpcode() == ISD::MUL) 89 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 90 91 // Look at Base + Index + Offset cases. 92 Index = Base->getOperand(1); 93 SDValue PotentialBase = Base->getOperand(0); 94 95 // Skip signextends. 96 if (Index->getOpcode() == ISD::SIGN_EXTEND) { 97 Index = Index->getOperand(0); 98 IsIndexSignExt = true; 99 } 100 101 // Check if Index Offset pattern 102 if (Index->getOpcode() != ISD::ADD || 103 !isa<ConstantSDNode>(Index->getOperand(1))) 104 return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt); 105 106 Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue(); 107 Index = Index->getOperand(0); 108 if (Index->getOpcode() == ISD::SIGN_EXTEND) { 109 Index = Index->getOperand(0); 110 IsIndexSignExt = true; 111 } else 112 IsIndexSignExt = false; 113 Base = PotentialBase; 114 } 115 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 116 } 117