+
+// SurveyFunction - This performs the initial survey of the specified function,
+// checking out whether or not it uses any of its incoming arguments or whether
+// any callers use the return value. This fills in the
+// (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
+//
+// We consider arguments of non-internal functions to be intrinsically alive as
+// well as arguments to functions which have their "address taken".
+//
+void DAE::SurveyFunction(Function &F) {
+ bool FunctionIntrinsicallyLive = false;
+ Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
+
+ if (!F.hasInternalLinkage() &&
+ (!ShouldHackArguments() || F.isIntrinsic()))
+ FunctionIntrinsicallyLive = true;
+ else
+ for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
+ // If this use is anything other than a call site, the function is alive.
+ CallSite CS = CallSite::get(*I);
+ Instruction *TheCall = CS.getInstruction();
+ if (!TheCall) { // Not a direct call site?
+ FunctionIntrinsicallyLive = true;
+ break;
+ }
+
+ // Check to see if the return value is used...
+ if (RetValLiveness != Live)
+ for (Value::use_iterator I = TheCall->use_begin(),
+ E = TheCall->use_end(); I != E; ++I)
+ if (isa<ReturnInst>(cast<Instruction>(*I))) {
+ RetValLiveness = MaybeLive;
+ } else if (isa<CallInst>(cast<Instruction>(*I)) ||
+ isa<InvokeInst>(cast<Instruction>(*I))) {
+ if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
+ !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
+ RetValLiveness = Live;
+ break;
+ } else {
+ RetValLiveness = MaybeLive;
+ }
+ } else {
+ RetValLiveness = Live;
+ break;
+ }
+
+ // If the function is PASSED IN as an argument, its address has been taken
+ for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
+ AI != E; ++AI)
+ if (AI->get() == &F) {
+ FunctionIntrinsicallyLive = true;
+ break;
+ }
+ if (FunctionIntrinsicallyLive) break;
+ }
+
+ if (FunctionIntrinsicallyLive) {
+ DOUT << " Intrinsically live fn: " << F.getName() << "\n";
+ for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
+ AI != E; ++AI)
+ LiveArguments.insert(AI);
+ LiveRetVal.insert(&F);
+ return;
+ }
+
+ switch (RetValLiveness) {
+ case Live: LiveRetVal.insert(&F); break;
+ case MaybeLive: MaybeLiveRetVal.insert(&F); break;
+ case Dead: DeadRetVal.insert(&F); break;
+ }
+
+ DOUT << " Inspecting args for fn: " << F.getName() << "\n";
+
+ // If it is not intrinsically alive, we know that all users of the
+ // function are call sites. Mark all of the arguments live which are
+ // directly used, and keep track of all of the call sites of this function
+ // if there are any arguments we assume that are dead.
+ //
+ bool AnyMaybeLiveArgs = false;
+ for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
+ AI != E; ++AI)
+ switch (getArgumentLiveness(*AI)) {
+ case Live:
+ DOUT << " Arg live by use: " << AI->getName() << "\n";
+ LiveArguments.insert(AI);
+ break;
+ case Dead:
+ DOUT << " Arg definitely dead: " << AI->getName() <<"\n";
+ DeadArguments.insert(AI);
+ break;
+ case MaybeLive:
+ DOUT << " Arg only passed to calls: " << AI->getName() << "\n";
+ AnyMaybeLiveArgs = true;
+ MaybeLiveArguments.insert(AI);
+ break;
+ }
+
+ // If there are any "MaybeLive" arguments, we need to check callees of
+ // this function when/if they become alive. Record which functions are
+ // callees...
+ if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
+ for (Value::use_iterator I = F.use_begin(), E = F.use_end();
+ I != E; ++I) {
+ if (AnyMaybeLiveArgs)
+ CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
+
+ if (RetValLiveness == MaybeLive)
+ for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
+ UI != E; ++UI)
+ InstructionsToInspect.push_back(cast<Instruction>(*UI));
+ }
+}
+
+// isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
+// know that the only uses of Arg are to be passed in as an argument to a
+// function call or return. Check to see if the formal argument passed in is in
+// the LiveArguments set. If so, return true.