commit 15467f5ca4d080ee9e5c28c881433d6ec6af33d8
Author: Tanya Lattner <tonic@nondot.org>
Date: Tue Mar 23 17:16:35 2010 +0000
Merge 99032 from mainline.
If call result is in ST0 and it is not being passed to the caller's
caller, then it is not safe to optimize the call into a sibcall since
the call result has to be popped off the x87 stack.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_27@99293 91177308-0d34-0410-b5e6-96231b3b80d8
commit d9fe4c59bba5baba38ba7bcf5282d72b72bccda4
Author: Tanya Lattner <tonic@nondot.org>
Date: Tue Mar 23 17:15:19 2010 +0000
Merge 98977 from mainline.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_27@99292 91177308-0d34-0410-b5e6-96231b3b80d8
commit 6d9848f18a63a1d7a9dbccfe74020af569c62eb2
Author: Tanya Lattner <tonic@nondot.org>
Date: Tue Mar 23 17:11:51 2010 +0000
Merge r98845 from mainline.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_27@99291 91177308-0d34-0410-b5e6-96231b3b80d8
commit 8c577d8dd275154fdd07bd048f200ab74cefd028
Author: Tanya Lattner <tonic@nondot.org>
Date: Tue Mar 23 17:06:56 2010 +0000
Merge 98561 from mainline.
Avoid sibcall optimization if either caller or callee is using sret semantics.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_27@99290 91177308-0d34-0410-b5e6-96231b3b80d8
commit 00d819742f6b1b80bcffccd94802788166ea1eef
Author: Tanya Lattner <tonic@nondot.org>
Date: Tue Mar 23 16:58:02 2010 +0000
Merge r98416 from mainline.
Fix a typo in ValueTracking that's causing instcombine to delete needed shift instructions.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_27@99289 91177308-0d34-0410-b5e6-96231b3b80d8
... | ... |
@@ -79,9 +79,9 @@ public: |
79 | 79 |
/// getEntryAlignment - Return the alignment of each entry in the jump table. |
80 | 80 |
unsigned getEntryAlignment(const TargetData &TD) const; |
81 | 81 |
|
82 |
- /// getJumpTableIndex - Create a new jump table or return an existing one. |
|
82 |
+ /// createJumpTableIndex - Create a new jump table. |
|
83 | 83 |
/// |
84 |
- unsigned getJumpTableIndex(const std::vector<MachineBasicBlock*> &DestBBs); |
|
84 |
+ unsigned createJumpTableIndex(const std::vector<MachineBasicBlock*> &DestBBs); |
|
85 | 85 |
|
86 | 86 |
/// isEmpty - Return true if there are no jump tables. |
87 | 87 |
/// |
... | ... |
@@ -779,7 +779,7 @@ unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD, |
779 | 779 |
for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
780 | 780 |
if (Tmp == 1) return Tmp; |
781 | 781 |
Tmp = std::min(Tmp, |
782 |
- ComputeNumSignBits(PN->getIncomingValue(1), TD, Depth+1)); |
|
782 |
+ ComputeNumSignBits(PN->getIncomingValue(i), TD, Depth+1)); |
|
783 | 783 |
} |
784 | 784 |
return Tmp; |
785 | 785 |
} |
... | ... |
@@ -203,7 +203,7 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF, |
203 | 203 |
MadeChange |= MadeChangeThisIteration; |
204 | 204 |
} |
205 | 205 |
|
206 |
- // See if any jump tables have become mergable or dead as the code generator |
|
206 |
+ // See if any jump tables have become dead as the code generator |
|
207 | 207 |
// did its thing. |
208 | 208 |
MachineJumpTableInfo *JTI = MF.getJumpTableInfo(); |
209 | 209 |
if (JTI == 0) { |
... | ... |
@@ -211,27 +211,8 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF, |
211 | 211 |
return MadeChange; |
212 | 212 |
} |
213 | 213 |
|
214 |
- const std::vector<MachineJumpTableEntry> &JTs = JTI->getJumpTables(); |
|
215 |
- // Figure out how these jump tables should be merged. |
|
216 |
- std::vector<unsigned> JTMapping; |
|
217 |
- JTMapping.reserve(JTs.size()); |
|
218 |
- |
|
219 |
- // We always keep the 0th jump table. |
|
220 |
- JTMapping.push_back(0); |
|
221 |
- |
|
222 |
- // Scan the jump tables, seeing if there are any duplicates. Note that this |
|
223 |
- // is N^2, which should be fixed someday. |
|
224 |
- for (unsigned i = 1, e = JTs.size(); i != e; ++i) { |
|
225 |
- if (JTs[i].MBBs.empty()) |
|
226 |
- JTMapping.push_back(i); |
|
227 |
- else |
|
228 |
- JTMapping.push_back(JTI->getJumpTableIndex(JTs[i].MBBs)); |
|
229 |
- } |
|
230 |
- |
|
231 |
- // If a jump table was merge with another one, walk the function rewriting |
|
232 |
- // references to jump tables to reference the new JT ID's. Keep track of |
|
233 |
- // whether we see a jump table idx, if not, we can delete the JT. |
|
234 |
- BitVector JTIsLive(JTs.size()); |
|
214 |
+ // Walk the function to find jump tables that are live. |
|
215 |
+ BitVector JTIsLive(JTI->getJumpTables().size()); |
|
235 | 216 |
for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); |
236 | 217 |
BB != E; ++BB) { |
237 | 218 |
for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); |
... | ... |
@@ -239,17 +220,14 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF, |
239 | 239 |
for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
240 | 240 |
MachineOperand &Op = I->getOperand(op); |
241 | 241 |
if (!Op.isJTI()) continue; |
242 |
- unsigned NewIdx = JTMapping[Op.getIndex()]; |
|
243 |
- Op.setIndex(NewIdx); |
|
244 | 242 |
|
245 | 243 |
// Remember that this JT is live. |
246 |
- JTIsLive.set(NewIdx); |
|
244 |
+ JTIsLive.set(Op.getIndex()); |
|
247 | 245 |
} |
248 | 246 |
} |
249 | 247 |
|
250 |
- // Finally, remove dead jump tables. This happens either because the |
|
251 |
- // indirect jump was unreachable (and thus deleted) or because the jump |
|
252 |
- // table was merged with some other one. |
|
248 |
+ // Finally, remove dead jump tables. This happens when the |
|
249 |
+ // indirect jump was unreachable (and thus deleted). |
|
253 | 250 |
for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i) |
254 | 251 |
if (!JTIsLive.test(i)) { |
255 | 252 |
JTI->RemoveJumpTable(i); |
... | ... |
@@ -594,17 +594,15 @@ unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const { |
594 | 594 |
return ~0; |
595 | 595 |
} |
596 | 596 |
|
597 |
-/// getJumpTableIndex - Create a new jump table entry in the jump table info |
|
598 |
-/// or return an existing one. |
|
597 |
+/// createJumpTableIndex - Create a new jump table entry in the jump table info. |
|
599 | 598 |
/// |
600 |
-unsigned MachineJumpTableInfo::getJumpTableIndex( |
|
599 |
+unsigned MachineJumpTableInfo::createJumpTableIndex( |
|
601 | 600 |
const std::vector<MachineBasicBlock*> &DestBBs) { |
602 | 601 |
assert(!DestBBs.empty() && "Cannot create an empty jump table!"); |
603 | 602 |
JumpTables.push_back(MachineJumpTableEntry(DestBBs)); |
604 | 603 |
return JumpTables.size()-1; |
605 | 604 |
} |
606 | 605 |
|
607 |
- |
|
608 | 606 |
/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update |
609 | 607 |
/// the jump tables to branch to New instead. |
610 | 608 |
bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, |
... | ... |
@@ -1674,11 +1674,10 @@ bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
1674 | 1674 |
} |
1675 | 1675 |
} |
1676 | 1676 |
|
1677 |
- // Create a jump table index for this jump table, or return an existing |
|
1678 |
- // one. |
|
1677 |
+ // Create a jump table index for this jump table. |
|
1679 | 1678 |
unsigned JTEncoding = TLI.getJumpTableEncoding(); |
1680 | 1679 |
unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
1681 |
- ->getJumpTableIndex(DestBBs); |
|
1680 |
+ ->createJumpTableIndex(DestBBs); |
|
1682 | 1681 |
|
1683 | 1682 |
// Set the jump table information so that we can codegen it as a second |
1684 | 1683 |
// MachineBasicBlock |
... | ... |
@@ -1477,7 +1477,6 @@ X86TargetLowering::LowerFormalArguments(SDValue Chain, |
1477 | 1477 |
DebugLoc dl, |
1478 | 1478 |
SelectionDAG &DAG, |
1479 | 1479 |
SmallVectorImpl<SDValue> &InVals) { |
1480 |
- |
|
1481 | 1480 |
MachineFunction &MF = DAG.getMachineFunction(); |
1482 | 1481 |
X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
1483 | 1482 |
|
... | ... |
@@ -1779,7 +1778,8 @@ X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee, |
1779 | 1779 |
|
1780 | 1780 |
if (isTailCall) { |
1781 | 1781 |
// Check if it's really possible to do a tail call. |
1782 |
- isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, |
|
1782 |
+ isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, |
|
1783 |
+ isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(), |
|
1783 | 1784 |
Outs, Ins, DAG); |
1784 | 1785 |
|
1785 | 1786 |
// Sibcalls are automatically detected tailcalls which do not require |
... | ... |
@@ -2297,6 +2297,8 @@ bool |
2297 | 2297 |
X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, |
2298 | 2298 |
CallingConv::ID CalleeCC, |
2299 | 2299 |
bool isVarArg, |
2300 |
+ bool isCalleeStructRet, |
|
2301 |
+ bool isCallerStructRet, |
|
2300 | 2302 |
const SmallVectorImpl<ISD::OutputArg> &Outs, |
2301 | 2303 |
const SmallVectorImpl<ISD::InputArg> &Ins, |
2302 | 2304 |
SelectionDAG& DAG) const { |
... | ... |
@@ -2316,10 +2318,37 @@ X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, |
2316 | 2316 |
// Look for obvious safe cases to perform tail call optimization that does not |
2317 | 2317 |
// requite ABI changes. This is what gcc calls sibcall. |
2318 | 2318 |
|
2319 |
- // Do not tail call optimize vararg calls for now. |
|
2319 |
+ // Do not sibcall optimize vararg calls for now. |
|
2320 | 2320 |
if (isVarArg) |
2321 | 2321 |
return false; |
2322 | 2322 |
|
2323 |
+ // Also avoid sibcall optimization if either caller or callee uses struct |
|
2324 |
+ // return semantics. |
|
2325 |
+ if (isCalleeStructRet || isCallerStructRet) |
|
2326 |
+ return false; |
|
2327 |
+ |
|
2328 |
+ // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack. |
|
2329 |
+ // Therefore if it's not used by the call it is not safe to optimize this into |
|
2330 |
+ // a sibcall. |
|
2331 |
+ bool Unused = false; |
|
2332 |
+ for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
|
2333 |
+ if (!Ins[i].Used) { |
|
2334 |
+ Unused = true; |
|
2335 |
+ break; |
|
2336 |
+ } |
|
2337 |
+ } |
|
2338 |
+ if (Unused) { |
|
2339 |
+ SmallVector<CCValAssign, 16> RVLocs; |
|
2340 |
+ CCState CCInfo(CalleeCC, false, getTargetMachine(), |
|
2341 |
+ RVLocs, *DAG.getContext()); |
|
2342 |
+ CCInfo.AnalyzeCallResult(Ins, RetCC_X86); |
|
2343 |
+ for (unsigned i = 0; i != RVLocs.size(); ++i) { |
|
2344 |
+ CCValAssign &VA = RVLocs[i]; |
|
2345 |
+ if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) |
|
2346 |
+ return false; |
|
2347 |
+ } |
|
2348 |
+ } |
|
2349 |
+ |
|
2323 | 2350 |
// If the callee takes no arguments then go on to check the results of the |
2324 | 2351 |
// call. |
2325 | 2352 |
if (!Outs.empty()) { |
... | ... |
@@ -637,6 +637,8 @@ namespace llvm { |
637 | 637 |
bool IsEligibleForTailCallOptimization(SDValue Callee, |
638 | 638 |
CallingConv::ID CalleeCC, |
639 | 639 |
bool isVarArg, |
640 |
+ bool isCalleeStructRet, |
|
641 |
+ bool isCallerStructRet, |
|
640 | 642 |
const SmallVectorImpl<ISD::OutputArg> &Outs, |
641 | 643 |
const SmallVectorImpl<ISD::InputArg> &Ins, |
642 | 644 |
SelectionDAG& DAG) const; |